home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap06 / DlgDemo2 / DlgDemo2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.7 KB  |  151 lines

  1. //***********************************************************************
  2. //
  3. //  DlgDemo2.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "DlgDemo2.h"
  10.  
  11. #define FONTHEIGHT 72
  12.  
  13. CMyApp myApp;
  14.  
  15. /////////////////////////////////////////////////////////////////////////
  16. // CMyApp member functions
  17.  
  18. BOOL CMyApp::InitInstance ()
  19. {
  20.     m_pMainWnd = new CMainWindow;
  21.     m_pMainWnd->ShowWindow (m_nCmdShow);
  22.     m_pMainWnd->UpdateWindow ();
  23.     return TRUE;
  24. }
  25.  
  26. /////////////////////////////////////////////////////////////////////////
  27. // CMainWindow message map and member functions
  28.  
  29. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  30.     ON_WM_ERASEBKGND ()
  31.     ON_WM_PAINT ()
  32.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  33.     ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     Create (NULL, "DlgDemo2", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  39.         MAKEINTRESOURCE (IDR_MAINFRAME));
  40. }
  41.  
  42. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  43. {
  44.     CRect rect;
  45.     GetClientRect (&rect);
  46.     DoGradientFill (pDC, &rect);
  47.     return TRUE;
  48. }
  49.  
  50. void CMainWindow::OnPaint ()
  51. {
  52.     CRect rect;
  53.     GetClientRect (&rect);
  54.  
  55.     CPaintDC dc (this);
  56.     DoDrawText (&dc, &rect);
  57. }
  58.  
  59. void CMainWindow::OnOptionsExit ()
  60. {
  61.     SendMessage (WM_CLOSE, 0, 0);
  62. }
  63.  
  64. void CMainWindow::OnOptionsAbout ()
  65. {
  66.     CAboutDialog dlg (this);
  67.     dlg.DoModal ();
  68. }
  69.  
  70. void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
  71. {
  72.     CBrush* pBrush[64];
  73.     for (int i=0; i<64; i++)
  74.         pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
  75.  
  76.     int nWidth = pRect->Width ();
  77.     int nHeight = pRect->Height ();
  78.     CRect rect;
  79.  
  80.     for (i=0; i<nHeight; i++) {
  81.         rect.SetRect (0, i, nWidth, i + 1);
  82.         pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
  83.     }
  84.  
  85.     for (i=0; i<64; i++)
  86.         delete pBrush[i];
  87. }
  88.  
  89. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  90. {
  91.     CFont font;
  92.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
  93.  
  94.     font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
  95.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  96.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
  97.  
  98.     pDC->SetBkMode (TRANSPARENT);
  99.     pDC->SetTextColor (RGB (255, 255, 255));
  100.  
  101.     CFont* pOldFont = pDC->SelectObject (&font);
  102.     pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER |
  103.         DT_VCENTER);
  104.  
  105.     pDC->SelectObject (pOldFont);
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////
  109. // CAboutDialog message map and member functions
  110.  
  111. BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
  112.     ON_WM_PAINT ()
  113. END_MESSAGE_MAP ()
  114.  
  115. BOOL CAboutDialog::OnInitDialog ()
  116. {
  117.     CDialog::OnInitDialog ();
  118.  
  119.     CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
  120.     pStatic->GetWindowRect (&m_rect);
  121.     pStatic->DestroyWindow ();
  122.     ScreenToClient (&m_rect);
  123.  
  124.     return TRUE;
  125. }
  126.  
  127. void CAboutDialog::OnPaint ()
  128. {
  129.     CPaintDC dc (this);
  130.     HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
  131.         GCL_HICON);
  132.  
  133.     if (hIcon != NULL) {
  134.         CDC dcMem;
  135.         dcMem.CreateCompatibleDC (&dc);
  136.  
  137.         CBitmap bitmap;
  138.         bitmap.CreateCompatibleBitmap (&dc, 32, 32);
  139.         CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
  140.  
  141.         CBrush brush (::GetSysColor (COLOR_3DFACE));
  142.         dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
  143.         dcMem.DrawIcon (0, 0, hIcon);
  144.  
  145.         dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
  146.             m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
  147.  
  148.         dcMem.SelectObject (pOldBitmap);
  149.     }
  150. }
  151.